using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interactor_ConfineVine_Ctrl : Unit_Interactor_Ctrl
{
    
    public FrameAnimation_Ctrl animation_Ctrl;

    public Transform confineVine_Renderer;

    public Sprite default_Sprite;

    public Unit_Charactor_Ctrl charactor_Ctrl;
    public bool isConfine;
    public void Init()
    {
        
        isConfine = false;
        renderer.sprite = default_Sprite;
    }
    //public void ReveiveLightTrigger(Collider2D collider)
    //{
    //    ReceiveLight(collider.transform.position, this.transform.position - collider.transform.position);
    //}
    public override void ReceiveLight(Vector3 sourcePos, Vector3 sourceDirection)
    {
        base.receiveLightEvent?.Invoke(sourcePos, sourceDirection);
        animation_Ctrl.Play("Dispel");
        charactor_Ctrl.moveLocks.Remove(this.GetInstanceID().ToString());
        charactor_Ctrl = null;
        isConfine = false;
        isActive = false;
    }
    protected override void OnUnitTriggerEnter(Unit_Base unit_Base)
    {
        if (!isActive)
        {
            return;
        }
        base.OnUnitTriggerEnter(unit_Base);
        switch (unit_Base.unitType)
        {
            case UnitType.Charactor:
                Unit_Charactor_Ctrl charactor_Ctrl = (Unit_Charactor_Ctrl)unit_Base;
                this.charactor_Ctrl = charactor_Ctrl;
                Vector3 initPos = charactor_Ctrl.transform.position;
                Vector3 targetPos = this.transform.position;
                animation_Ctrl.Play("Confine");
                isConfine = true;
                charactor_Ctrl.moveLocks.Add(this.GetInstanceID().ToString());
                IEPool_Manager.instance.KeepTimeToDo("", 0.16f, null, (_time) =>
                {
                    charactor_Ctrl.transform.position = Vector3.Lerp(initPos, targetPos, 1 - _time / 0.16f);
                    if (!isConfine)
                    {
                        return false;
                    }
                    return true;
                },()=> {
                    charactor_Ctrl.transform.position = targetPos;
                });
                break;
        }
    }
}
